package cn.tanghom.baiding.service.system.service;

import cn.tanghom.baiding.api.system.model.Organization;
import cn.tanghom.baiding.api.system.model.User;
import cn.tanghom.baiding.api.system.service.UserService;
import cn.tanghom.baiding.core.service.impl.BaseServiceImpl;
import cn.tanghom.baiding.core.utils.CommonUtils;
import cn.tanghom.baiding.core.utils.Md5Util;
import cn.tanghom.baiding.core.utils.StringUtils;
import cn.tanghom.baiding.service.system.dao.OrganizationDao;
import cn.tanghom.baiding.service.system.dao.UserDao;
import cn.tanghom.baiding.service.system.dao.UserRoleDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

import javax.annotation.Resource;

/**
 * 用户管理逻辑处理层
 *
 * @author tanghom<tanghom@qq.com> on 2016/5/16
 */
@Service("userService")
public class UserServiceImpl extends BaseServiceImpl<User> implements UserService {

    @Resource
    private UserDao userDao;
    @Resource
    private UserRoleDao userRoleDao;
    @Resource
    private OrganizationDao organizationDao;

    @Override
    public String saveOrUpdate(User user, Long[] roleId) {
        try {
            User checkUser = userDao.selectByLoginName(user.getLoginName());
            Organization organization = organizationDao.selectByPrimaryKey(user.getOrganizationId());
            if (user.getId() == null) {// 添加
                if (checkUser != null) {
                    return CommonUtils.msgCallback(false, "登录名已存在,请换一个!", "", null);
                }
                String pwd = user.getLoginPwd();
                if (StringUtils.isNullOrEmpty(pwd)) {
                    pwd = "123456";
                }
                pwd = Md5Util.encrypt(pwd);
                user.setLoginPwd(pwd);
                user.setOrganizationId(organization.getId());
                user.setOrganizationName(organization.getName());
                user.setCreateTime(new Date());
                userDao.insert(user);

            } else {// 修改
                User u = userDao.selectByPrimaryKey(user.getId());
                if (checkUser != null && u.getId() != checkUser.getId()) {
                    return CommonUtils.msgCallback(false, "登录名已存在,请换一个!", "", null);
                }
                u.setUserName(user.getUserName());
                u.setLoginName(user.getLoginName());
                String pwd = user.getLoginPwd();
                if (!StringUtils.isNullOrEmpty(pwd)) {
                    pwd = Md5Util.encrypt(pwd);
                    u.setLoginPwd(pwd);
                }
                u.setSex(user.getSex());
                u.setStatus(user.getStatus());
                u.setOrganizationId(organization.getId());
                u.setOrganizationName(organization.getName());
                u.setPhone(user.getPhone());
                userDao.update(u);
                userRoleDao.delete(user.getId(), null);
            }
            if (null != roleId) {
                int roleIdSize = roleId.length;
                for (int i = 0; i < roleIdSize; i++) {
                    userRoleDao.save(user.getId(), roleId[i]);
                }
            }
            return CommonUtils.msgCallback(true, "操作成功", "", null);
        } catch (Exception e) {
            throw e;
        }
    }

    @Override
    public void reg(User user) {
        //默认角色消防站
        user.setLoginPwd(Md5Util.encrypt(user.getLoginPwd()));
        user.setCreateTime(new Date());
        userDao.insert(user);
        userRoleDao.save(user.getId(), 3L);
    }

    @Override
    public User selectByLoginName(String loginName) {
        User user = userDao.selectByLoginName(loginName);
        return user;
    }
}
